package team.scau.laboratory_repair.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import team.scau.laboratory_repair.web.interceptor.LoginInterceptor;

import java.io.File;

/**
 * @author 30254
 * creadtedate:2018/8/17
 */
@Configuration
public class WebConf implements WebMvcConfigurer {

    public static final String UPLOAD_PATH = new File("upload/").getAbsolutePath();

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ UPLOAD_PATH + File.separator);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/pc/**").excludePathPatterns("/**");
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".html");
        resolver.setViewNames("/**");
        resolver.setContentType("text/html;charset=UTF-8");
        resolver.setOrder(1);
        registry.viewResolver(resolver);
    }
}
